package es.xpt.ysf.jaas;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.jboss.aop.Advisor;
import org.jboss.aop.InstanceAdvisor;
import org.jboss.aop.advice.AspectFactory;
import org.jboss.aop.joinpoint.Joinpoint;
import org.jboss.ejb3.security.SecurityDomainManager;
import org.jboss.security.RealmMapping;

public class RoleBasedAuthorizationInterceptorFactory implements AspectFactory { // NO_UCD
	public Object createPerVM() {
		throw new RuntimeException(
				"PER_VM not supported for this interceptor factory, only PER_CLASS");
	}

	
	public Object createPerClass(Advisor advisor) {
		
		return new RoleBasedAuthorizationInterceptor();
	}
	
	public static RealmMapping createRealmMapping(InitialContext ctx, String securityAnotationValue) {
		Object domain = null;
		try {
			domain = SecurityDomainManager.getSecurityManager(securityAnotationValue, ctx);
		} catch (NamingException e) {
			throw new RuntimeException(e);
		}
		RealmMapping mapping = (RealmMapping) domain;
		return mapping;
	}

	public Object createPerInstance(Advisor advisor,
			InstanceAdvisor instanceAdvisor) {
		throw new RuntimeException(
				"PER_VM not supported for this interceptor factory, only PER_CLASS");
	}

	public Object createPerJoinpoint(Advisor advisor, Joinpoint jp) {
		throw new RuntimeException(
				"PER_VM not supported for this interceptor factory, only PER_CLASS");
	}

	public Object createPerJoinpoint(Advisor advisor,
			InstanceAdvisor instanceAdvisor, Joinpoint jp) {
		throw new RuntimeException(
				"PER_VM not supported for this interceptor factory, only PER_CLASS");
	}

	public String getName() {
		return getClass().getName();
	}

}
